﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Ausschluss : BaseImageFilter, ITwoSourceFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Ausschluss"/> class.
        /// </summary>
        public Ausschluss()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Ausschluss"/> class.
        /// </summary>
        /// <param name="_ausschluss">The _ausschluss.</param>
        internal Ausschluss(Ausschluss _ausschluss)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// Processes the specified _bitmap source.
        /// </summary>
        /// <param name="_bitmapSource">The _bitmap source.</param>
        /// <param name="_bitmapDest">The _bitmap dest.</param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmapSource, UnsafeBitmap _bitmapDest)
        {
            UnsafeBitmap bitmapSource = _bitmapSource;
            UnsafeBitmap bitmapDest = _bitmapDest;

            Color colorSource = Color.Transparent;
            Color colorDest = Color.Transparent;

            float fFactor = 0.00196850394f;

            int y = 0;
            int x = 0;

            int nHeight = bitmapSource.Height;
            int nWidth = bitmapSource.Width;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    colorSource = bitmapSource.GetPixel(x, y);
                    colorDest = bitmapDest.GetPixel(x, y);
                    
                    float nRSource = colorSource.R;
                    float nGSource = colorSource.G;
                    float nBSource = colorSource.B;
                    float nRDest = colorDest.R;
                    float nGDest = colorDest.G;
                    float nBDest = colorDest.B;

                    nRSource = nRSource + nRDest - 2 * nRSource * nRDest;
                    nGSource = nGSource + nRDest - 2 * nGSource * nGDest;
                    nRSource = nBSource + nRDest - 2 * nBSource * nBDest;

                    nRSource *= fFactor;
                    nGSource *= fFactor;
                    nBSource *= fFactor;

                    colorSource = Color.FromArgb((int)-nRSource, (int)-nGSource, (int)-nBSource);
                    bitmapDest.SetPixel(x, y, colorSource);
                }
            }
            bitmapSource.Dispose();
            bitmapDest.Dispose();

            return _bitmapDest;
        }

        #endregion

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Ausschluss(this);
        }
    }
}
